const { root } = require("../config.json");

let settingsData = { global: { sections: {} }, pages_settings: [] };
let settingsScheme = { global_setting: [], pages_setting: [], blocks_setting: [] };

//缓存积木块配置信息
let blocksSetting = {};

function createSetting(path) {
    // let settingsData = { global: { sections: {} }, pages_settings: [] };
    // let settingsScheme = { global_setting: [], pages_setting: [], blocks_setting: [] };
    // //缓存积木块配置信息
    // let blocksSetting = getDefaultBlock();
    // settingsScheme.blocks_setting = getblock(path, blocksSetting);

    // `./src/theme/${root}/lib/config/page.json`
    // `./src/theme/${root}/lib/config/page.json`
}

//获取默认积木块配置
function getDefaultBlock() {
    const defaultDirPath = `./src/theme/${root}/sections`;
    const filenames = fs.readdirSync(defaultDirPath);
    filenames.forEach(function (file) {
        const content = JSON.parse(fs.readFileSync(defaultDirPath + "/" + file + "/index.json", "utf-8"));
        blocksSetting[file] = content.schema;
    });
}

//设置全局配置
function getGlobal(path) {
    const global = JSON.parse(fs.readFileSync( path, "utf-8"));
    settingsScheme.global_setting = global;
    global.forEach((element) => {
        element.settings.forEach((setting) => {
            settingsData.global[setting.id] = setting.default;
        });
    });
}

//设置积木块信息
function getblock(path, blocksSetting) {
    const blockContent = JSON.parse(fs.readFileSync(path, "utf-8"));
    settingsScheme.blocks_setting = blockContent.map((blocks) => {
        blocks.sections = blocks.sections.map((block) => {
            if (blocksSetting[block]) {
                return {
                    name: blocksSetting[block].name,
                    type: "block",
                    icon: blocksSetting[block].icon,
                    routes: blocksSetting[block].routes || [],
                    id: block
                };
            }

            return {
                type: "block",
                id: block
            };
        });
        return blocks;
    });
}

function setRouterSections(sections) {
    return sections.map((section) => {
        return {
            type: section.type,
            fixed: section.fixed === "top" ? 1 : section.fixed === "bottom" ? 3 : 2,
            params: section.params ? section.params.default : blocksSetting[section.type].default
        };
    });
}

//获取页面配置
function getPage(path) {
    const pageContent = JSON.parse(fs.readFileSync(path, "utf-8"));
    pageContent.forEach((element, index) => {
        settingsData.pages_settings.push({
            route: element.route,
            route_handle: element.route_handle,
            sections: setRouterSections(element.sections)
        });

        settingsScheme.pages_setting.push({
            name: element.name,
            route: element.route,
            max_block: element.max_block,
            route_handle: element.route_handle,
            sections: []
        });
        element.sections.forEach((setting) => {
            settingsScheme.pages_setting[index].sections.push({
                type: setting.type,
                fixed: setting.fixed,
                name: blocksSetting[setting.type].name || {}
            });
        });

        if (element.layout) {
            settingsScheme.pages_setting[index].sections.unshift({
                type: "header",
                fixed: "top",
                name: {
                    zh_CN: "页头"
                }
            });
            settingsScheme.pages_setting[index].sections.push({
                type: "footer",
                fixed: "bottom",
                name: {
                    zh_CN: "页尾"
                }
            });
        }
    });
}
